package nocuffin.m5;


import java.io.*;

import android.os.*;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/** 
 * Team: No Cuffin'
 * 2/27/2012
 * 
 *
 * This class controls I/O for determining valid users and other functions associated with storing, checking, and managing 
 * user information. 
 * 
 * 
 * @author Robert Allen, Philip Johnson, Steven Moore
 * @version 1.0 2/27/2012
 *
 */ 
public class NewUserScreen extends Activity{
	Button bCreate, bCancel;
    TextView titleLabel, userLabel, passwordLabel, passwordagainLabel;
    EditText usernameText, passwordText, passwordtextagain;
    UserMaster mast;
    final String FILENAME = "key";

	@Override
	 public void onCreate(Bundle savedInstanceState) {
		 
		 super.onCreate(savedInstanceState);
	     setContentView(R.layout.main2);
	   
	     bCreate = (Button) findViewById(R.id.createButton);
	     bCancel = (Button) findViewById (R.id.cancelButton);
	     titleLabel = (TextView) findViewById (R.id.titleLabel);
	     userLabel = (TextView) findViewById (R.id.userLabel);
	     passwordLabel = (TextView) findViewById (R.id.passwordLabel);
	     passwordagainLabel = (TextView) findViewById (R.id.passwordagainLabel);
	     usernameText = (EditText) findViewById (R.id.usernameText);
	     passwordText = (EditText) findViewById (R.id.passwordText);
	     passwordtextagain = (EditText) findViewById (R.id.passwordTextagain);
	     mast = new UserMaster(); //UserMaster created!!
	    
	     bCancel.setOnClickListener(new View.OnClickListener() {
	            
	        	
	        	/** 
	        	 * 
	        	 * Manipulates the text boxes upon a click of the available button. 
	        	 * 
	        	 * 
	        	 * @param v Default android param
	        	 *
	        	 */ 
	        	public void onClick(View v) {
	        		 Intent myIntent = new Intent(v.getContext(), M5Activity.class);
	                 startActivity(myIntent);
	            }
	        });
	     bCreate.setOnClickListener(new View.OnClickListener() {
	            
	        	
	        	/** 
	        	 * 
	        	 * Manipulates the text boxes upon a click of the available button. 
	        	 * 
	        	 * 
	        	 * @param v Default android param
	        	 *
	        	 */ 
	    		public void onClick(View v) {
	    		
	    			int ifvalid = 0;
	    			String userdata = usernameText.getText().toString();
	    			String passworddata = passwordText.getText().toString();
	    			String passworddataagain = passwordtextagain.getText().toString();
	    			
	    			if (passworddata.equals(passworddataagain))
	    			{
	    				ifvalid = 1;
	    			}
	    			else
	    			{
	    				titleLabel.setText("Passwords didn't match.");
	    			}
	    			
	    			if ((usernameText.getText().toString() + passwordText.getText().toString()).length() < 7)
	    			{
	    	 			ifvalid = 0;
	    	 			titleLabel.setText("Not enough characters - extend password or username.");
	    			}
	    			if (ifvalid == 1)
	    			{
	    				//This (data) is what's written to the file
	    				String data = userdata + passworddata + "\n";
	    				//String collected = null;
	    				FileOutputStream fos;
	    				try 
	    				{
	    					fos = new FileOutputStream(Environment.getExternalStorageDirectory());
	    				} catch (FileNotFoundException e1) {
	    				// TODO Auto-generated catch block
	    					e1.printStackTrace();
	    				}
	    			
	    				try 
	    				{
	    					fos = openFileOutput(FILENAME, Context.MODE_APPEND);
	    					fos.write(data.getBytes());
	    					fos.flush();
	    					fos.close();
	    				} 
	    				catch (FileNotFoundException e) {
	    					// TODO Auto-generated catch block
	    					e.printStackTrace();
	    				} catch (IOException e) {
	    					// TODO Auto-generated catch block
	    					e.printStackTrace();
	    				}
	    				usernameText.setText("");
	    			    passwordText.setText(""); 
	    			    passwordtextagain.setText("");
	    			    Intent myIntent = new Intent(v.getContext(), M5Activity.class);
		                startActivity(myIntent);
	    			    
	    			
	    			}
	    		
	    			
	    			
	    			
	        
	     
	    		}

		
	    		
	     });
	     
	}
	
}
	
	     
	     
	    		
	     
	   
	        		
	                 
	     
	           
	     
	 
	

